import commJS from '../common.js';
import richText from '../../../src/page/common/richText';

export default {
  components: {
    richText
  },
  data() {
    return {
      //  表单
      form_data: {
        page:'',
        image:[],
        type:"1",
        major_text_position:'',
        major_text_type:'1',
        major_text:[],
        major_text1:'',
        txt_title:"",
        video_url:'',
        video_text:'',
        content:'',
        bg_image:[]
      },
      restaurants: [],
      kw: "",
      rules: {
        page: [{
          required: true,
          message: '请输入第几页',
          trigger: 'blur',
        }],
      },
      eid:'',
      // chars:'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
      chars:'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',
      smallLesson_id:"",
      smallLesson_title:'',
      // 七牛云地址
      upload_img_url: this.adminApi.upload_url,
      postData: {},
      domain: '',
    }
  },

  /**
   * 进入页面加载
   */
  mounted: function () {
    var that = this;
    commJS.getQiNiuToken(that);
    var query = that.$route.query;
    if (query.smallLesson_id) {
      that.smallLesson_id = query.smallLesson_id;
      that.smallLesson_title=query.smallLesson_name;
      that.eid=query.id;
      if(that.eid){
        that.getDetail();
      }
    }
    // that.getTitle();
    // this.getcode(10);
  },

  //方法
  methods: {
    /**
     * 获取详情
     */
    getDetail() {
      var that = this;
      that.axios.post("/ebookPage/edit", {
        id: that.eid
      }).then(data => {
        if (data) {
          that.form_data.page = data.page;
          that.form_data.type = String(data.type);
          that.form_data.image = [{
            url: data.image
          }];


         that.form_data.major_text_type=data.major_text_type.toString();
          if(data.major_text_type==1){
            if(data.major_text){
              that.form_data.major_text=[{
                url:data.major_text
              }]
            }else{
              that.form_data.major_text=[];
            }

            that.form_data.major_text1='';
          }else{
            that.form_data.major_text=[];
            that.form_data.major_text1=data.major_text;
          }
          that.form_data.major_text_position=data.major_text_position.toString();

          that.form_data.txt_title  = data.txt_title;
          that.form_data.video_url   = data.video_url;
          that.form_data.video_text   = data.video_text ;
          that.form_data.content    = data.content;//文字内容(需要编辑器)
          if(data.bg_image){
            that.form_data.bg_image   = [{
              url:data.bg_image
            }];
          }else{
            that.form_data.bg_image=[];
          }

        }
      });
    },
    /**
     * 富文本改变时
     */
    editor_change(e) {
      this.form_data.content = e;
    },
    editor_change1(e) {
      this.form_data.major_text1 = e;
    },
    editor_change2(e) {
      this.form_data.txt_title = e;
    },
    /**
     * 图片上传成功
     */
    img_succ(res) {
      const that = this;
      that.form_data.image.push({
        url: that.domain + res.key,
      })
    },
    img_succ1(res) {
      const that = this;
      that.form_data.major_text.push({
        url: that.domain + res.key,
      })
    },
    img_succ2(res) {
      const that = this;
      that.form_data.bg_image.push({
        url: that.domain + res.key,
      })
    },
    /**
     *图片移除
     */
    del_img(file, fileList) {
      this.form_data.image = fileList
    },
    del_img1(file, fileList) {
      this.form_data.major_text = fileList
    },
    del_img2(file, fileList) {
      this.form_data.bg_image = fileList
    },
    /**
     * 文件超出个数限制时的钩子
     */
    descExceed() {
      this.$message.warning('只能上传一张图片哦!');
    },
    descExceed1() {
      this.$message.warning('只能上传一张图片哦!');
    },
    descExceed2() {
      this.$message.warning('只能上传一张图片哦!');
    },
    save() {
      const that = this;
      that.$refs.form_data.validate((valid) => {
        if (!valid) return that.$message.warning('请完整填写内容!');
        var formData = {};

        formData.eid = that.smallLesson_id;

        formData.id=that.eid;
        console.log(formData.id)
        formData.page = that.form_data.page;
        formData.type = that.form_data.type; //1图 2图+短文 3图+长文 4图+视频 5纯文字）
        if(that.form_data.image.length>0){
          formData.image   = that.form_data.image[0].url;
        }else{
          formData.image   = '';
        }
        formData.major_text_type=that.form_data.major_text_type;
        if(formData.type==2){
          formData.major_text_type=2;
        }
        if((formData.type==1||formData.type==4)&&formData.major_text_type==1){
          formData.major_text=that.form_data.major_text[0].url
        }else{
          formData.major_text=that.form_data.major_text1;
        }

        formData.major_text_position=that.form_data.major_text_position;

        formData.txt_title  = that.form_data.txt_title;
        formData.video_url   = that.form_data.video_url;
        formData.video_text   = that.form_data.video_text ;
        formData.content    = that.form_data.content;//文字内容(需要编辑器)
        if(that.form_data.bg_image.length>0){
          formData.bg_image   = that.form_data.bg_image[0].url
        }else{
          formData.bg_image   = '';
        }

        var url = '/ebookpage/create';
        if (that.eid) {
          url = '/ebookpage/update';
        }
        that.axios.post(url, formData).then(() => {

          that.$message.success(that.banner_id ? '修改成功' : '添加成功');
          that.$router.go(-1)
        });
      })
    },
    /**
     * 返回
     */
    back() {
      this.$router.go(-1);
    },
  }
}
